package hbtDAO;

import hbt.HibernateUtil;

import java.util.ArrayList;
import java.util.List;

import model.Tema;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;


public class HibernateTemaDAO {
	private static HibernateTemaDAO instancia;
	
	private static SessionFactory sf;

	public static HibernateTemaDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia=new HibernateTemaDAO();
		} 
		return instancia;
	}
	
	@SuppressWarnings("unchecked")
	public List<Tema> TemasList(){
		Session session = sf.openSession();
		Query q = session.createQuery("from Tema p");
		List<Tema> listaDeTemas = new ArrayList<Tema>(q.list());
		session.close();
		return listaDeTemas;
	}
	
	public void guardarUnTema(Tema t){
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(t); 
		session.getTransaction().commit();
		session.close();		
	}	
	
	public void borrarTema(Tema t){
		Session session = sf.openSession();
		session.beginTransaction();
		session.delete(t); 
		session.getTransaction().commit();
		session.close();
	}
	
	public Tema obtenerTema(long idTema){
		Session session = sf.openSession();
		Query q = session.createQuery("FROM Tema u WHERE u.idTema = :idTema").setString("idTema", String.valueOf(idTema));
		Tema t = (Tema) q.uniqueResult();
		session.close();
		return t;
	}
}